package sunwul.hospital.hosp.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import sunwul.hospital.hosp.mapper.CommonMapper;
import sunwul.hospital.common.exception.HospitalGlobalException;
import sunwul.hospital.common.result.ResultCodeEnum;
import sunwul.hospital.common.utils.MD5;

import java.util.Map;

/**
 * @author sunwul
 * @date 2023/11/4 17:16:20
 * @description 通用校验
 */
@Component
public class CheckUtil {

    @Autowired
    CommonMapper commonMapper;


    /**
     * 校验hoscode及对应的signKey
     * @param map 参数map
     * @return 校验结果
     */
    public boolean checkSignKey(Map<String, Object> map){
        if (map.get("hoscode") == null || map.get("sign") == null){
            throw new HospitalGlobalException(ResultCodeEnum.PARAM_ERROR);
        }
        String hoscode = (String) map.get("hoscode");
        String hospSign = (String) map.get("sign"); //service-manage模块中的signkey进行了二次MD5加密,对比时需要将数据库中的signkey也加密一下
        // 查询数据库中signKey
        String key = commonMapper.findHospitalSetSignKey(hoscode);
        if (!hospSign.equals(MD5.encrypt(key))){
            throw new HospitalGlobalException(ResultCodeEnum.SIGN_ERROR);
        }
        return true;
    }
}
